package unimelb.distSystems.project2;

/*
 * Lim Ching Lih, 308721
 * Yu Xia, 351025
 * Carlos Vasconcelos, 193554
 */

import java.util.Properties;

import unimelb.distSystems.communications.CORBA.*;

import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;

/*
 * COBRA client class
 */
public class GameClient {
    public Game game;
    public ClientService cref;

    public GameClient(Game g,String address,int port_no) {
        game = g;
        
        try{
            // create and initialize the ORB
        	Properties props = new Properties();
            props.put("org.omg.CORBA.ORBInitialPort", port_no);
            props.put("org.omg.CORBA.ORBInitialHost", address);
            ORB orb = ORB.init(new String[]{}, props);

            // get the root naming context
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");

            // Use NamingContextExt instead of NamingContext, part of the Interoperable naming Service.
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            // resolve the Object Reference in Naming
            GameService gameService = GameServiceHelper.narrow(ncRef.resolve_str("CrashTestDummyGame"));
            game.setGameService(gameService);
            
            
            // SET CALLBACK
            POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
                rootpoa.the_POAManager().activate();
            ClientServiceImpl clientService = new ClientServiceImpl(this);
            clientService.setORB(orb);
            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(clientService);
            cref = ClientServiceHelper.narrow(ref);

        }catch(Exception e){}
    }
}

